<?php

namespace app\api\controller;

use app\admin\model\Activity;
use app\admin\model\Address;
use app\admin\model\Apply;
use app\admin\model\Cart;
use app\admin\model\Cate;
use app\admin\model\Consigneeaddress;
use app\admin\model\Cost;
use app\admin\model\Coupon;
use app\admin\model\Distance;
use app\admin\model\Expert;
use app\admin\model\Goods;
use app\admin\model\Ordderdistribution;
use app\admin\model\Ordderdistributionaddress;
use app\admin\model\Order;
use app\admin\model\Ordergoods;
use app\admin\model\Usercoupon;
use app\common\controller\Api;
use app\common\model\MoneyLog;
use app\common\utils\FeiUtil;
use app\common\utils\StripeUtil;
use app\common\utils\Util;
use app\common\utils\WechatPayUtil;
use Stripe\StripeClient;
use think\Config;
use think\Db;
use think\Log;

/**
 * b1048接口
 */
class Index extends Api
{
    protected $noNeedLogin = ['cate', 'goodsList', 'test', 'recommendGoodsList', 'banner','rechargeOpen'];
    protected $noNeedRight = ['*'];

    /**
     * 首页
     * @ApiInterval()
     */
    public function test()
    {
        $id = 31;
        $type = 1;
        $order = Order::where('id', $id)->where('pay_status', 1)->find();
        if (!$order) {
            $this->error("订单不存在");
        }
        $pay = new StripeUtil();
        $payData = $pay->pay('Mall payment', $order->id, $type);
        if (!$payData) {
            $this->error("支付失败");
        }
        $order->pay_url = $payData->url;
        $res = $order->save();
        echo '<pre>';
        var_dump($res);
        print_r($payData);


    }

    /**
     * 首页轮播图
     * @ApiMethod (GET)
     */
    public function banner()
    {
        $banner = Config::get('site.index_images');
        $this->success("success", compact('banner'));
    }

    /**
     * 产品分类
     * @ApiMethod (GET)
     * @ApiReturnParams (name="id", type="int", required=true, description="分类id")
     */
    public function cate()
    {
        $data = Cate::where('is_open',2)->order('id asc')->select();
        $this->success("success", $data);
    }

    /**
     * 商品列表
     * @ApiMethod (GET)
     * @ApiParams (name="cate_id", type="int", required=true, description="分类id")
     * @ApiParams (name="page", type="int", required=true, description="页码")
     * @ApiParams (name="limit", type="int", required=true, description="每页数量")
     * @ApiParams (name="name", type="string", required=false, description="商品名称")
     * @ApiParams (name="sales_num", type="string", required=false, description="销量排序:asc=升序,desc=降序")
     * @ApiParams (name="money", type="string", required=false, description="价格排序:asc=升序,desc=降序")
     */
    public function goodsList()
    {
        $cate_id = $this->request->get("cate_id");
        $page = $this->request->get("page", 1);
        $limit = $this->request->get("limit", 10);
        $name = $this->request->get("name");
        $sales_num = $this->request->get("sales_num");
        $money = $this->request->get("money");
        $order = "";
        if ($sales_num) {
            $order .= "sales_num $sales_num,";
        }
        if ($money) {
            $order .= "money $money,";
        }
        $order .= "id desc";
//        print_r($order);die;
        $where = [];
        if ($name) {
            $where['name'] = ['like', "%{$name}%"];
        }
        if ($cate_id) {
            $where['cate_id'] = $cate_id;
        }
        $data = Goods::where($where)
            ->where('is_open',1)
            ->order($order)
            ->page($page, $limit)
            ->select();
        foreach ($data as &$item) {
            $car = Cart::where('goods_id', $item['id'])
                ->where('user_id', $this->auth->id)
                ->find();
            $item['is_cart'] = $car ? 1 : 0;
            $item['car_num'] = $car ? $car['num'] : 0;
        }
        $this->success("success", $data);
    }

    /**
     * 商品详情
     * @ApiMethod (GET)
     * @ApiParams (name="goods_id", type="int", required=true, description="商品id")
     */
    public function goodsDetail()
    {
        $id = $this->request->get("goods_id");
        $data = Goods::where('id', $id)->find();
        $car = Cart::where('goods_id', $id)
            ->where('user_id', $this->auth->id)
            ->find();
        $data['is_cart'] = $car ? 1 : 0;
        $data['car_num'] = $car ? $car['num'] : 0;
        $this->success("success", $data);
    }


    /**
     * 推荐商品列表
     * @ApiMethod (GET)
     * @ApiParams (name="page", type="int", required=true, description="页码")
     * @ApiParams (name="limit", type="int", required=true, description="每页数量")
     */
    public function recommendGoodsList()
    {
        $page = $this->request->get("page", 1);
        $limit = $this->request->get("limit", 10);
        $data = Goods::where('is_recommend', 1)
            ->where('is_open',1)
            ->order('id desc')
            ->page($page, $limit)
            ->select();
        $this->success("success", $data);
    }


    /**
     * 商品加入购物车
     * @ApiMethod (POST)
     * @ApiParams (name="goods_id", type="int", required=true, description="商品id")
     * @ApiParams (name="num", type="int", required=true, description="数量")
     */
    public function addCart()
    {
        $goods_id = $this->request->post("goods_id");
        $num = $this->request->post("num");
        $user_id = $this->auth->id;
        $goods = Goods::where('id', $goods_id)->find();
        if (!$goods) {
            $this->error("商品不存在");
        }
        $cart = Cart::where('user_id', $user_id)
            ->where('goods_id', $goods_id)
            ->find();
        if ($cart) {
            if ($num == 0) {
                $cart->delete();
            }
            $cart->num = $num;
        } else {
            if ($num == 0) {
                $this->error("传参错误");
            }
            $cart = new Cart();
            $cart->user_id = $user_id;
            $cart->goods_id = $goods_id;
            $cart->num = $num;
        }
        try {
            $cart->save();
        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }
        $this->success("加入成功");
    }

    /**
     * 购物车列表
     * @ApiMethod (GET)
     */
    public function cartList()
    {
        $user_id = $this->auth->id;
        $model = new Cart();
        $data = $model->alias('a')
            ->join('goods b', 'a.goods_id=b.id')
            ->where('a.user_id', $user_id)
            ->field('a.*,b.name,b.money,b.image,b.unit,b.freight_money')
            ->order('id desc')
            ->select();
        $this->success("success", $data);
    }

    /**
     * 购物车删除
     * @ApiMethod (POST)
     * @ApiParams (name="ids", type="string", required=true, description="购物车id,多个用逗号隔开")
     */
    public function cartDel()
    {
        $ids = $this->request->post("ids");
        $user_id = $this->auth->id;
        $model = new Cart();
        $model->where('user_id', $user_id)
            ->where('id', 'in', $ids)
            ->delete();
        $this->success("删除成功");
    }

    /**
     * 购物车修改数量
     * @ApiMethod (POST)
     * @ApiParams (name="id", type="int", required=true, description="购物车id")
     * @ApiParams (name="num", type="int", required=true, description="数量")
     */
    public function cartEdit()
    {
        $id = $this->request->post("id");
        $num = $this->request->post("num");
        $user_id = $this->auth->id;
        $model = new Cart();
        $cart = $model->where('user_id', $user_id)
            ->where('id', $id)
            ->find();
        if (!$cart) {
            $this->error("购物车不存在");
        }
        $cart->num = $num;
        try {
            $cart->save();
        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }
        $this->success("修改成功");
    }

    /**
     * 购物车结算
     * @ApiMethod (POST)
     * @ApiParams (name="ids", type="string", required=true, description="购物车id,多个用逗号隔开")
     * @ApiParams (name="address_id", type="int", required=true, description="地址id")
     * @ApiParams (name="coupon_id", type="int", required=false, description="优惠券id")
     *
     */
    public function cartPay()
    {
        $ids = $this->request->post("ids");
        $address_id = $this->request->post("address_id");
        $coupon_id = $this->request->post("coupon_id");
        $address = Address::where('id', $address_id)->find();
        if (!$address) {
            $this->error("地址不存在");
        }
        $user_id = $this->auth->id;
        $model = new Cart();
        $data = $model->alias('a')
            ->join('goods b', 'a.goods_id=b.id')
            ->where('a.user_id', $user_id)
            ->where('a.id', 'in', $ids)
            ->field('a.*,b.name,b.money,b.image,b.freight_money,b.stock')
            ->order('id desc')
            ->select();
        if (!$data) {
            $this->error("购物车不存在");
        }

        Db::startTrans();
        try {
            $order_no = getOrderSn();

            $order_goods_data = [];
            $total_money = 0;
            $total_freight_money = 0;
            $num = 0;
            foreach ($data as $item) {
                $goods= Goods::where('id', $item['goods_id'])->find();
                if ($goods->stock < $item['num']) {
                   throw new \Exception($goods['name']."商品库存不足");
                }
                $goods->stock=$goods->stock-$item['num'];
                $goods->save();
                $money = $item['money'] * $item['num'];
                $total_money += $money;
                $total_freight_money = $item['freight_money'];
                $num += $item['num'];
                $order_goods_data[] = [
                    'order_no' => $order_no,
                    'goods_id' => $item['goods_id'],
                    'goods_name' => $item['name'],
                    'goods_image' => $item['image'],
                    'goods_money' => $item['money'],
                    'freight_money' => $item['freight_money'],
                    'num' => $item['num'],
                    'money' => $money,
                ];
            }
            $order_data = [
                'user_id' => $user_id,
                'order_no' => $order_no,
                'address_id' => $address_id,
//            'address_info' => "姓名:".$address['username'] . '_手机号:' . $address['mobile'] . '_地址:' . $address['address'],
                'address_info' => "姓名:" . $address['username'] . ',手机号:' . $address['mobile'] . ',地址:' . $address['address'],
                'goods_money' => $total_money,
                'discount_money' => 0,
                'delivery_cost' => $total_freight_money,
                'money' => $total_money + $total_freight_money,
                'goods_num' => $num,
                'pay_status' => 2,
                'pay_time' => time()
            ];
            $user = \app\common\model\User::where('id', $user_id)->find();
            if ($user->money < $order_data['money']) {
                throw new \Exception("余额不足");
            }
            $after = $user->money - $order_data['money'];


            $order = new Order();
            $order->save($order_data);
            MoneyLog::create(['user_id' => $user_id, 'money' => '-' . $order_data['money'], 'before' => $user->money, 'after' => $after, 'memo' => '购买商品,订单号：' . $order_no]);
            $user->money = $after;
            $user->save();
            $order_goods = new OrderGoods();

            $order_goods->saveAll($order_goods_data);

            $model->where('user_id', $user_id)
                ->where('id', 'in', $ids)
                ->delete();
//            $pay = new WechatPayUtil();
//            $payData = $pay->pay($order_no, $total_money, $this->auth->openid, 1);
//            if (!$payData) {
//                Db::rollback();
//                throw new \Exception("支付失败");
//            }
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            $this->error($e->getMessage());
        }
        $content = "【五六购物】你有新的订单了," . $order_data['address_info'];
        $mobile = Config::get('site.sms_mobile');
        Util::smsbao($content, $mobile);
        $this->success("购买成功");
    }

    /**
     * 立即支付
     * @ApiMethod (POST)
     * @ApiParams (name="id", type="int", required=true, description="订单id")
     * @ApiParams (name="type", type="int", required=true, description="支付类型：1订单 2配送")
     */
    public function pay()
    {
        $id = $this->request->post("id");
        $type = $this->request->post("type", 1);
        if ($type == 1) {
            $order = Order::where('id', $id)->where('pay_status', 1)->find();
        } else {
            $order = Ordderdistribution::where('id', $id)->where('pay_status', 1)->find();
        }
        if (!$order) {
            $this->error("Order does not exist");
        }
        $pay = new StripeUtil();
        $payData = $pay->pay('Mall payment', $order->id, $type);
        if (!$payData) {
            $this->error("支付失败");
        }
        $order->pay_url = $payData->url;
        $order->save();
        $this->success("success", $order);
    }


    /**
     * 订单列表
     * @ApiMethod (GET)
     * @ApiParams (name="pay_status", type="int", required=true, description=":支付状态:1=待支付,2=待发货,3=已发货,4=已完成")
     * @ApiParams (name="page", type="int", required=true, description="页码")
     * @ApiParams(name="limit", type="int", required=true, description="每页数量")
     */
    public function orderList()
    {
        $pay_status = $this->request->get("pay_status");
        $page = $this->request->get("page");
        $limit = $this->request->get("limit");
        $user_id = $this->auth->id;
        $where = [];
        if ($pay_status) {
            $where['pay_status'] = $pay_status;
        }
        $model = new Order();
        $data = $model
            ->with('ordergoods')
            ->where('user_id', $user_id)
            ->where($where)
            ->order('id desc')
            ->page($page, $limit)
            ->select();
        $this->success(__('fasong'), $data);
    }

    /**
     * 订单详情
     * @ApiMethod (GET)
     * @ApiParams (name="id", type="int", required=true, description="订单id")
     */
    public function orderDetail()
    {
        $id = $this->request->get("id");
        $user_id = $this->auth->id;
        $model = new Order();
        $order = $model->with('ordergoods')
            ->where('user_id', $user_id)
            ->where('id', $id)
            ->find();
        if (!$order) {
            $this->error("订单不存在");
        }
        $order->address_info = json_decode($order->address_info, true);
//        $order->createtime = date("Y-m-d H:i:s", $order->createtime);
        $this->success("success", $order);
    }


    /**
     * 订单取消
     * @ApiMethod (POST)
     * @ApiParams (name="id", type="int", required=true, description="订单id")
     * @ApiParams (name="remark", type="string", required=true, description="取消原因")
     */
    public function orderCancel()
    {
        if ($this->request->isPost()) {
            $id = $this->request->post("id");
            $remark = $this->request->post("remark");
            $user_id = $this->auth->id;
            $model = new Order();
            $order = $model->where('user_id', $user_id)
                ->where('id', $id)
                ->find();
            if (!$order) {
                $this->error("订单不存在");
            }
            if ($order['pay_status'] == 1) {
                $order->delete();
                $this->success("取消成功");
            }
            if ($order['pay_status'] != 2) {
                $this->error("订单状态不可取消");
            }
            $order->remark = $remark;
            $order->pay_status = 3;
            try {
                $order->save();
            } catch (\Exception $e) {
                $this->error($e->getMessage());
            }
            $this->success("取消成功");
        }
    }


    /**
     * 地址添加OR修改
     * @ApiMethod (POST)
     * @ApiParams (name="address", type="string", required=true, description="详细地址")
     * @ApiParams (name="dorm", type="string", required=true, description="宿舍")
     * @ApiParams (name="username", type="string", required=true, description="姓名")
     * @ApiParams (name="gender", type="int", required=true, description=":1=先生,2=女士")
     * @ApiParams (name="mobile", type="string", required=true, description="手机号")
     * @ApiParams (name="id", type="int", required=false, description="地址id,修改时传")
     * @ApiParams (name="is_default", type="int", required=false, description="是否默认:1=是,2=否")
     */
    public function addressAdd()
    {
        if ($this->request->isPost()) {
            $id = $this->request->post("id");
            $address_dz = $this->request->post("address");
            $dorm = $this->request->post("dorm");
            $username = $this->request->post("username");
            $gender = $this->request->post("gender");
            $mobile = $this->request->post("mobile");
            $is_default = $this->request->post("is_default");
            if ($is_default == 1) {
                Address::where('user_id', $this->auth->id)
                    ->update(['is_default' => 2]);
            }
            if ($id) {
                $address = Address::where('user_id', $this->auth->id)
                    ->where('id', $id)
                    ->find();
                if (!$address) {
                    $this->error("地址不存在");
                }
                $address->address = $address_dz;
                $address->dorm = $dorm;
                $address->username = $username;
                $address->gender = $gender;
                $address->mobile = $mobile;

                if ($is_default == 2 && $address->is_default == 1) {
                    $address->is_default = 1;
                } else {
                    $address->is_default = $is_default;
                }
            } else {
                $address = new Address();
                $address->user_id = $this->auth->id;
                $address->address = $address_dz;
                $address->dorm = $dorm;
                $address->username = $username;
                $address->gender = $gender;
                $address->mobile = $mobile;
                $address->is_default = $is_default;
            }
            try {
                $address->save();
            } catch (\Exception $e) {
                $this->error($e->getMessage());
            }
            $this->success("success");
        }
    }

    /**
     * 地址删除
     * @ApiMethod (POST)
     * @ApiParams (name="id", type="int", required=true, description="地址id")
     */
    public function addressDel()
    {
        if ($this->request->isPost()) {
            $id = $this->request->post("id");
            $address = Address::where('user_id', $this->auth->id)
                ->where('id', $id)
                ->find();
            if (!$address) {
                $this->error("地址不存在");
            }
            try {
                $address->delete();
            } catch (\Exception $e) {
                $this->error($e->getMessage());
            }
            $this->success("success");
        }
    }

    /**
     * 获取地址详情
     * @ApiMethod (GET)
     * @ApiParams (name="id", type="int", required=true, description="地址id")
     */
    public function addressDetail()
    {
        $id = $this->request->get("id");
        if (!$id) {
            $address = Address::where('user_id', $this->auth->id)
                ->where('is_default', 1)->find();
        } else {
            $address = Address::where('user_id', $this->auth->id)
                ->where('id', $id)->find();
        }

        if (!$address) {
            $this->error("地址不存在");
        }
        $this->success("success", $address);
    }

    /**
     * 地址列表
     * @ApiMethod (GET)
     * @ApiParams (name="page", type="int", required=true, description="页码")
     * @ApiParams(name="limit", type="int", required=true, description="每页数量")
     */
    public function addressList()
    {
        $page = $this->request->get("page", 1);
        $limit = $this->request->get("limit", 10);
        $user_id = $this->auth->id;
        $model = new Address();
        $data = $model
            ->where('user_id', $user_id)
            ->order('id desc')
            ->page($page, $limit)
            ->select();
        $this->success("success", $data);
    }

    /**
     * 选择收件地址列表
     * @ApiMethod (GET)
     */
    public function consigneeList()
    {
        $address = Consigneeaddress::order('id asc')
            ->select();
        $this->success("success", $address);
    }

    /**
     * 快递站地址列表
     * @ApiMethod (GET)
     */
    public function deposit()
    {
        $data = Db::name('deposit_address')->order('id asc')->select();
        $this->success("success", $data);
    }

    /**
     * 创建地址订单
     * @ApiMethod (POST)
     * @ApiParams (name="get_address_id", type="string", required=true, description="取件地址ID")
     * @ApiParams (name="shou_address_id", type="string", required=true, description="收件地址ID")
     * @ApiParams (name="distance_id", type="int", required=true, description="距离ID")
     * @ApiParams (name="type", type="int", required=true, description="类型:1=帮我送,2=帮我取")
     * @ApiParams (name="remark", type="int", required=true, description="备注")
     */
    public function createOrder()
    {
        $data = $this->request->post();
        if (empty($data['get_address_id']) || empty($data['shou_address_id']) || empty($data['distance_id'])) {
            $this->error("参数错误");
        }
        $distance = Distance::where('id', $data['distance_id'])->find();
        if (!$distance) {
            $this->error("距离不存在");
        }
        unset($data['distance_id']);
        $get_address = Address::where('user_id', $this->auth->id)
            ->where('id', $data['get_address_id'])
            ->find();
        if (!$get_address) {
            $this->error("取件地址不存在");
        }
        unset($data['get_address_id']);
        $shou_address = Address::where('user_id', $this->auth->id)
            ->where('id', $data['shou_address_id'])
            ->find();
        if (!$shou_address) {
            $this->error("收件地址不存在");
        }
        unset($data['shou_address_id']);
        $data['get_address'] = $get_address['username'] . '_' . $get_address['mobile'] . '_' . $get_address['address'];
        $data['shou_address'] = $shou_address['username'] . '_' . $shou_address['mobile'] . '_' . $shou_address['address'];
        $data['juli'] = $distance['name'];
        $data['money'] = $distance['money'];
        $data['user_id'] = $this->auth->id;
        $order_no = getOrderSn();
        $data['order_no'] = $order_no;
        unset($v);
        $model = new Ordderdistribution();
        $model->save($data);
        $this->success("success", $model);
    }

    /**
     * 地址订单列表
     * @ApiMethod (GET)
     * @ApiParams (name="page", type="int", required=true, description="页码")
     * @ApiParams(name="limit", type="int", required=true, description="每页数量")
     * @ApiParams(name="pay_status", type="int", required=true, description="支付状态:支付状态:1=待支付,2=待配送,3=已配送,4=已完成")
     */
    public function orderAdreesList()
    {
        $page = $this->request->get("page", 1);
        $limit = $this->request->get("limit", 10);
        $pay_status = $this->request->get("pay_status", 0);
        $where = [];
        if ($pay_status) {
            $where['pay_status'] = $pay_status;
        }
        $data = Ordderdistribution::where('user_id', $this->auth->id)
            ->where($where)
            ->order('id desc')
            ->page($page, $limit)
            ->select();
        $this->success("success", $data);
    }


    /**
     * 问题列表
     * @ApiMethod (GET)
     */
    public function questionList()
    {
        $data = Db::name('question')
            ->order('id desc')
            ->select();
        $this->success("success", $data);
    }

    /**
     * 意见反馈
     * @ApiMethod (POST)
     * @ApiParams (name="lx", type="int", required=true, description="")
     * @ApiParams (name="describe", type="string", required=true, description="描述")
     */
    public function feedback()
    {
        $lx = $this->request->post('lx');
        $describe = $this->request->post('describe');
        if (!$describe) {
            $this->error("参数错误");
        }
        $data = [
            'user_id' => $this->auth->id,
            'describe' => $describe,
            'lx' => $lx,
            'createtime' => time(),
        ];
        Db::name('idea')->insert($data);
        $this->success("success");
    }

    /**
     * 获取用户信息
     * @ApiMethod (GET)
     */
    public function userInfo()
    {
        $user = $this->auth->getUserinfo();
        $this->success("success", $user);
    }

    /**
     * 确认收货
     * @ApiMethod (POST)
     * @ApiParams (name="id", type="int", required=true, description="订单ID")
     * @ApiParams (name="type", type="int", required=true, description="类型:1=商品订单,2=地址订单")
     */
    public function confirmOrder()
    {
        $type = $this->request->post('type', 1);
        $id = $this->request->post('id');
        if (!$id) {
            $this->error("参数错误");
        }
        if ($type == 1) {
            $model = new Order();
        } else {
            $model = new Ordderdistribution();
        }
        $order = $model->where('id', $id)->find();
        if (!$order) {
            $this->error("订单不存在");
        }
//        if ($order->pay_status != 3) {
//            $this->error("订单状态错误");
//        }
        $order->pay_status = 4;
        $order->save();
        $this->success("操作成功");
    }

    /**
     * 地址订单详情
     * @ApiMethod (GET)
     * @ApiParams (name="id", type="int", required=true, description="订单ID")
     */
    public function orderAdreesInfo()
    {
        $id = $this->request->get('id');
        if (!$id) {
            $this->error("参数错误");
        }
        $order = Ordderdistribution::where('id', $id)->find();
        if (!$order) {
            $this->error("订单不存在");
        }
        $this->success("success", $order);
    }

    /**
     * 优惠券列表
     * @ApiMethod (GET)
     */
    public function coupon()
    {
        $ids = Usercoupon::where('user_id', $this->auth->id)->where('status', 1)->column('coupon_id');
        $model = new Coupon();
        $data = $model
            ->where('id', 'not in', $ids)
            ->select();
        $this->success('请求成功', $data);
    }

    /**
     * 领取优惠券
     * @ApiMethod (POST)
     * @ApiParams (name="id", type="string", required=true, description="优惠券id")
     */
    public function getCoupon()
    {
        $coupon_id = $this->request->post('id');
        if (!$coupon_id) {
            $this->error('参数错误');
        }
        $model = new Coupon();
        if ($find = $model->where('id', $coupon_id)->find()) {
            $data = [
                'user_id' => $this->auth->id,
                'coupon_id' => $coupon_id,
                'discount_money' => $find['discount_money'],
                'total_money' => $find['total_money'],
                'name' => $find['name'],
                'beg_time' => strtotime(date('Y-m-d')),
                'end_time' => strtotime(date('Y-m-d', strtotime('+1 month')))
            ];
            Usercoupon::create($data);
            $this->success('领取成功');
        } else {
            $this->error('优惠券不存在');
        }
    }

    /**
     * 我的优惠券
     * @ApiMethod (GET)
     * @ApiParams (name="status", type="string", required=true, description="状态 1未使用 2已使用 3已过期")
     */
    public function myCoupon()
    {
        $model = new Usercoupon();
        $where = [];
        $stauts = $this->request->get('status', 1);
        if ($stauts) {
            $where['status'] = $stauts;
        }
        $data = $model
            ->where('user_id', $this->auth->id)
            ->where($where)
            ->select();
        $this->success('请求成功', $data);
    }


    /**
     * 兑换卷卡
     * @ApiMethod (POST)
     * @ApiParams (name="number", type="string", required=true, description="兑换码")
     * @ApiParams (name="password", type="string", required=true, description="密码")
     */
    public function duiCost()
    {
        if ($this->request->isPost()) {
            $number = $this->request->post('number');
            $password = $this->request->post('password');
            $cost = Cost::where('number', $number)
                ->where('password', $password)
                ->where('status', 1)->find();
            if (empty($cost)) {
                $this->error('兑换码或密码错误');
            }
            \app\common\model\User::money($cost['money'], $this->auth->id, '兑换卡兑换');
            $cost->user_id = $this->auth->id;
            $cost->status = 2;
            $cost->save();
            $this->success('兑换成功');
        }
    }


    /**
     * 快递查询
     * @ApiMethod (GET)
     * @ApiParams (name="id", type="string",required=true, description="订单ID")
     */
    public function kuaidi()
    {
        $id = $this->request->get('id');
        $order = Order::get($id);
        $res = Util::kuaidi100($order->kd_com, $order->kd_no);
        $this->success('ok', $res);
    }

    /**
     * 活动列表
     * @ApiMethod (GET)
     * @ApiParams (name="page", type="string",required=true, description="页码")
     * @ApiParams (name="limit", type="string",required=true, description="每页条数")
     */
    public function activityList()
    {
        $page = $this->request->get('page', 1);
        $limit = $this->request->get('limit', 10);
        $model = new Activity();
        $data = $model->order('id desc')
            ->page($page, $limit)->select();
        $this->success('ok', $data);
    }

    /**
     * 活动详情
     * @ApiMethod (GET)
     * @ApiParams (name="id", type="string",required=true, description="活动ID")
     */
    public function activityInfo()
    {
        $id = $this->request->get('id');
        $model = new Activity();
        $data = $model->where('id', $id)->find();
        $this->success('ok', $data);
    }

    /**
     * 比赛报名
     * @ApiMethod (POST)
     * @ApiParams (name="id", type="string",required=true, description="活动ID")
     * @ApiParams (name="username", type="string",required=true, description="姓名")
     * @ApiParams (name="age", type="string",required=true, description="年龄")
     * @ApiParams (name="sex", type="string",required=true, description="性别")
     * @ApiParams (name="nation", type="string",required=true, description="民族")
     * @ApiParams (name="mobile", type="string",required=true, description="电话")
     * @ApiParams (name="content", type="string",required=true, description="活动内容")
     * @ApiParams (name="images", type="string",required=true, description="参赛照片")
     */
    public function signUp()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $activity = Activity::get($data['id']);
            if (!$activity) {
                $this->error('活动不存在');
            }
            $data['activity_id'] = $data['id'];
            $data['user_id'] = $this->auth->id;
            $data['tj_time'] = time();
            $data['bm_time'] = $activity['bm_time'];
            $data['ks_time'] = $activity['ks_time'];
            unset($data['id']);
            $model = new Apply();
            $model->save($data);
            $this->success('报名成功');
        }
    }

    /**
     * 达人添加
     * @ApiMethod (POST)
     * @ApiParams (name="name", type="string",required=true, description="姓名")
     * @ApiParams (name="image", type="string",required=true, description="图片")
     * @ApiParams (name="jieshao", type="string",required=true, description="介绍")
     */
    public function star()
    {
        if ($this->request->isPost()) {
            $model = new Expert();
            $name = $this->request->post('name');
            $image = $this->request->post('image');
            $jieshao = $this->request->post('jieshao');
            $data = [
                'user_id' => $this->auth->id,
                'name' => $name,
                'image' => $image,
                'jieshao' => $jieshao
            ];
            $model->save($data);
            $this->success('添加成功');
        }
    }

    /**
     * 达人列表
     * @ApiMethod (GET)
     * @ApiParams (name="page", type="string",required=true, description="页码")
     * @ApiParams (name="limit", type="string",required=true, description="每页条数")
     */
    public function starList()
    {
        $page = $this->request->get('page', 1);
        $limit = $this->request->get('limit', 10);
        $model = new Expert();
        $data = $model->order('id desc')
            ->page($page, $limit)->select();
        $this->success('ok', $data);
    }

    /**
     * 达人详情
     * @ApiMethod (GET)
     * @ApiParams (name="id", type="string",required=true, description="达人ID")
     */
    public function starInfo()
    {
        $id = $this->request->get('id');
        $model = new Expert();
        $data = $model->where('id', $id)->find();
        $this->success('ok', $data);
    }

    public function rechargeOpen(){
        $status = Config::get('site.recharge_open');
        $this->success('ok', $status);
    }
}
